AM_SRCS := platform/nemu/trm.c \
		   platform/nemu/ioe/ioe.c \
		   platform/nemu/ioe/timer.c \
		   platform/nemu/ioe/input.c \
		   platform/nemu/ioe/gpu.c \
		   platform/nemu/ioe/audio.c \
		   platform/nemu/ioe/disk.c \
		   platform/nemu/mpe.c
# 修改较大
CFLAGS    += -fdata-sections -ffunction-sections
LDFLAGS   += -T $(AM_HOME)/scripts/linker.ld \
			 --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0
LDFLAGS   += --gc-sections -e _start
NEMUFLAGS += --elf=$(IMAGE).elf --log=$(shell dirname $(IMAGE).elf)/nemu-log.txt

CFLAGS += -DMAINARGS=\"$(mainargs)\"
CFLAGS += -I$(AM_HOME)/am/src/platform/nemu/include
.PHONY: $(AM_HOME)/am/src/platform/nemu/trm.c

image: $(IMAGE).elf
	@$(OBJDUMP) -d $(IMAGE).elf > $(IMAGE).txt
	@echo + OBJCOPY "->" $(IMAGE_REL).bin
	@$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(IMAGE).elf $(IMAGE).bin

ARGS_LOG ?= --log=$(shell dirname $(IMAGE).elf)/nemu-log.txt
ARGS_IMG ?= $(IMAGE).bin
ARGS_ELF ?= --elf=$(IMAGE).elf
ARGS_APPNAME ?= 
ARGS_RAMDISK ?= 

# 它会覆盖掉 NEMU 的默认参数选项
NEMU_ARGS = ARGS_LOG="$(ARGS_LOG)" ARGS_ELF="$(ARGS_ELF)" ARGS_RAMDISK=$(ARGS_RAMDISK) ARGS_APPNAME=$(ARGS_APPNAME) ARGS_IMG="$(ARGS_IMG)" 

run: image
	$(MAKE) -C $(NEMU_HOME) ISA=$(ISA) run $(NEMU_ARGS)

gdb: image
	$(MAKE) -C $(NEMU_HOME) ISA=$(ISA) gdb $(NEMU_ARGS)

gdb-remote: image
	$(MAKE) -C $(NEMU_HOME) ISA=$(ISA) gdb-remote $(NEMU_ARGS)
